package schoolmath.web.services;

import org.apache.tapestry5.OptionGroupModel;
import org.apache.tapestry5.OptionModel;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.SelectModelVisitor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SelectServiceImpl implements SelectService {

	@Override
	public <T> OptionModel createOptionModel(final T object) {
		return new OptionModel() {
			@Override
			public String getLabel() {
				return object.toString();
			}

			@Override
			public boolean isDisabled() {
				return false;
			}

			@Override
			public Map<String, String> getAttributes() {
				return null;
			}

			@Override
			public Object getValue() {
				return object;
			}
		};
	}

	@Override
	public <T> List<OptionModel> createOptionModelList(final List<T> objects) {
		List<OptionModel> list = new ArrayList<>();

		for (T obj : objects) {
			list.add(createOptionModel(obj));
		}

		return list;
	}

	@Override
	public SelectModel createSelectModel(final List<OptionModel> optionModelList) {
		return new SelectModel() {
			@Override
			public List<OptionGroupModel> getOptionGroups() {
				return null;
			}

			@Override
			public List<OptionModel> getOptions() {
				return optionModelList;
			}

			@Override
			public void visit(SelectModelVisitor visitor) {
				for (OptionModel optionModel : optionModelList) {
					visitor.option(optionModel);
				}
			}
		};
	}

}
